#ifndef LOG_H_
#define LOG_H_

#include <syslog.h>

#include "main.h"

#if DBG
# include <stdio.h>

# define OPENLOG(cmd)
# define LOG(level, fmt, args...) \
    fprintf(stderr, "%s:%d: "fmt"\n", __FILE__, __LINE__, ## args);

# define LOG_DATA(level, datap, datalen, fmt, args...) \
    do { \
        char __str__[(datalen) * 3 + 1]; \
        void *__dptr__; \
        size_t __i__, __n__; \
        for (__dptr__ = (datap), __i__ = 0; __i__ < (size_t)(datalen); ++__i__) { \
            __n__ = 3 * __i__; \
            snprintf(__str__ + __n__, sizeof(__str__) - __n__, "%02hhX ", *(unsigned char *) __dptr__++); \
        } \
        LOG((level), fmt": %s", ## args, __str__); \
    } while (0)

void print_data(const char *msg, const void *data, ssize_t datalen);
# define PRINT_DATA(msg, datap, datalen) \
        print_data((msg), (datap), (datalen))

#else

# define OPENLOG(cmd) openlog(cmd, LOG_CONS, LOG_DAEMON)
# define LOG syslog
# define LOG_DATA(level, datap, datalen, fmt, args...)
# define PRINT_DATA(msg, datap, datalen)

#endif

#endif /* LOG_H_ */
